package com.ziyun.erp.web.api.quotePrintFactory.controller;

import com.utility.JsonUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.quotePrint.service.QuotePrintService;
import com.ziyun.erp.modules.quotePrintFactory.service.QuotePrintFactoryService;
import com.ziyun.erp.modules.quotePrintFactory.vo.QuotePrintFactoryEntityVo;
import com.ziyun.erp.modules.quotePrintFactoryPrice.entity.QuotePrintFactoryPriceEntity;
import com.ziyun.erp.modules.quotePrintFactoryPrice.service.QuotePrintFactoryPriceService;
import io.renren.common.utils.R;
import net.sf.json.util.JSONUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 工厂印刷机配置表API
 *
 * @author zh
 * @email
 * @date 2018-10-18 16:32:19
 */
@RestController
@RequestMapping("port/quotePrintFactory/quoteprintfactory")
public class QuotePrintFactoryApi {
    private  static Logger logger = LoggerFactory.getLogger(QuotePrintFactoryApi.class);

    @Autowired
    private QuotePrintFactoryService quotePrintFactoryService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map paramBodyMap){
        logger.info("==============api请求参数："+ paramBodyMap +" ================");
        Map<String,Object> rsData = this.quotePrintFactoryService.listQuotePrintFactory(paramBodyMap);

        return R.ok().put("page", rsData.get("page")).put("userData",rsData.get("userData"));
    }



    /**
     * 返回工厂以及配价信息
     */
    @RequestMapping("/info")
    public R info(@RequestBody Map paramBodyMap){

        Integer id = Integer.parseInt(paramBodyMap.get("id").toString());
        QuotePrintFactoryEntityVo  quotePrintFactoryVo = this.quotePrintFactoryService.getInfo(id);

        return R.ok().put("quotePrintFactoryVo", quotePrintFactoryVo);
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Map paramBodyMap){
        //删除quote_print_factory表中数据，修改表中的del_flag字段
        List<Integer> idList = JsonUtils.string2Collection(paramBodyMap.get("ids").toString(), List.class, Integer.class);
        //创建一个Map封装修改的参数
        Map<String,Object> map=new HashMap<String, Object>();
       // map.put("update_user",UserUtils.getCurentUser().getUsername());
        Integer[] ids = new Integer[]{};
        ids = idList.toArray(ids);
        this.quotePrintFactoryService.deleteQuotePrintFactory(ids, paramBodyMap);
        return R.ok();
    }

    /**
     * 保存打印机及其价格
     */
    @RequestMapping("/saveWithPrice")
    public R savePrintFacotoryWithPrintPrice(@RequestBody Map paramBodyMap){

        QuotePrintFactoryEntityVo quotePrintFactoryVo= com.ziyun.erp.common.utils.JsonUtils.string2Object(paramBodyMap.get("quotePrintFactoryVo").toString(),QuotePrintFactoryEntityVo.class);
        String createName=paramBodyMap.get("createName").toString();
        return quotePrintFactoryService.save(quotePrintFactoryVo,createName);
    }

    /**
     * 修改打印机及其价格
     */
    @RequestMapping("/updateWithPrice")
    public R update(@RequestBody QuotePrintFactoryEntityVo quotePrintFactoryVo){
       /* ValidatorUtils.validateEntity(quotePrintFactory);*/
       return quotePrintFactoryService.update(quotePrintFactoryVo);//全部更新
    }

    /**
     * 修改打印机是否前台展示状态
     */
    @RequestMapping("/updateDisplay")
    public R updateDisplay(@RequestBody Map paramBodyMap){
        //先根据id查出来，再更改状态
        Integer id = Integer.parseInt(paramBodyMap.get("id").toString());
        this.quotePrintFactoryService.updateDisplay(id);

        return R.ok();
    }
}
